package org.example.mrrope.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.ListUtils;
import org.example.mrrope.utils.MapUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.example.mrrope.utils.MapUtils.mapStringToMap;

/**
 * Remittance
 *
 * @author 陈韵琴
 * @since 2024/7/3 下午2:57
 *
 * 回款单赋值进去
 */
@Service
@Slf4j
public class Remittance {

    @Autowired
    private DingUtil dingUtil;

    /**
     * 回款单赋值进销售订单
     */
    @Async
    public void remittanceSetSales(String formId) throws Exception {
        //获取到汇款单的表单信息
        String remittance = dingUtil.getYidaById(formId);
        //转为JSONObject
        JSONObject jsonObject = new JSONObject(remittance);
        //获取里面的子表单,判空
        if (jsonObject.has("tableField_lxbhzlw5")) {
            JSONArray jsonArrayA = jsonObject.getJSONArray("tableField_lxbhzlw5");
            for (int i = 0; i < jsonArrayA.length(); i++) {
                JSONObject jsonObjectC = new JSONObject();
                JSONObject jsonObjectB = jsonArrayA.getJSONObject(i);
                jsonObjectC.put("dateField_lup17akg", jsonObject.has("dateField_luowsk7j")?jsonObject.get("dateField_luowsk7j"):"");//收款日期
                Double scys = 0.0;
                Double syje = 0.0;
                //上次剩余应收
                if (jsonObjectB.has("numberField_ly5ilowc")){
                    scys = jsonObjectB.getDouble("numberField_ly5ilowc");
                }
                //本次收款金额
                if (jsonObjectB.has("numberField_lxbhzlw6")){
                    syje = jsonObjectB.getDouble("numberField_lxbhzlw6");
                }
                //总金额
                Double countmoneyNumber = 0.0;
                if (jsonObjectB.has("numberField_luowsk7c")){
                    countmoneyNumber = jsonObjectB.getDouble("numberField_luowsk7c");
                }
                jsonObjectC.put("numberField_lup17akh", scys);//上次剩余应收
                jsonObjectC.put("numberField_lup17aki", syje);//本次收款金额
                jsonObjectC.put("numberField_lycg2410", countmoneyNumber-scys+syje);//收款后剩余金额
                jsonObjectC.put("textField_lup17akj", jsonObjectB.has("selectField_luowsk7q")?jsonObjectB.get("selectField_luowsk7q"):"");//收款账户
                jsonObjectC.put("textField_lxbg8a07", jsonObjectB.has("textareaField_luowsk7v")?jsonObjectB.get("textareaField_luowsk7v"):"");//备注
                String instanceId = null;

                //获取到so号
                if (jsonObjectB.has("associationFormField_luowsk70_id")) {
                    String str = jsonObjectB.getString("associationFormField_luowsk70_id");
                    String StrTow = str.substring(1, str.length() - 1);
                    String res = StringEscapeUtils.unescapeJava(StrTow);
                    JSONArray newMap = new JSONArray(res);
                    JSONObject newMapA = newMap.getJSONObject(0);
                    instanceId = newMapA.getString("instanceId");
                }
                log.info(instanceId);
                //查询销售订单的数据
                String yidaById = dingUtil.getYidaById(instanceId);
                //转为JSONObject
                JSONObject jsonObjectD = new JSONObject(yidaById);
                JSONObject jsonObjectE = new JSONObject();
                JSONArray jsonArrayOne = new JSONArray();
                //累计收款
                Double moneyNumber = 0.0;
                if (jsonObjectD.has("tableField_lup17akf")) {
                    //拿到销售订单子表的数据
                    jsonArrayOne = jsonObjectD.getJSONArray("tableField_lup17akf");
                    jsonArrayOne.put(jsonObjectC);
                    for (int j = 0; j < jsonArrayOne.length(); j++) {
                        moneyNumber = moneyNumber + jsonArrayOne.getJSONObject(j).getDouble("numberField_lup17aki");//总金额
                    }
                    log.info("moneyNumber-----------"+moneyNumber);
                }else {
                    moneyNumber = syje;
                    jsonArrayOne.put(jsonObjectC);
                }
                jsonObjectE.put("tableField_lup17akf", jsonArrayOne);
                String type = null;
                if (jsonObjectB.has("selectField_lxbgai1s")) {
                    String typeOne = (String) jsonObjectB.get("selectField_lxbgai1s");
                    if (typeOne.equals("全款")) {
                        type = "已完成";
                    } else if (typeOne.equals("尾款")) {
                        type = "已完成";
                    } else if (typeOne.equals("预付款")) {
                        type = "进行中";
                    }
                }
                jsonObjectE.put("textField_lxlrx5lm", type);
                //待收款金额
                jsonObjectE.put("numberField_ly46scyj",  countmoneyNumber - moneyNumber);
                //已收款金额
                jsonObjectE.put("numberField_ly46scyk", moneyNumber);
                //修改这条数据
                dingUtil.UpdateYiDa(instanceId, jsonObjectE);
            }
        }
    }
}
